//
//  SEGSettingDoorView.m
//  YYKitDemo
//
//  Created by 刘山 on 2017/11/9.
//  Copyright © 2017年 Sam. All rights reserved.
//

#import "SEGSettingDoorView.h"

#if __has_include(<Commom/WBTextLinePositionModifier.h>)
#import <Commom/WBTextLinePositionModifier.h>
#else
#import "WBTextLinePositionModifier.h"
#endif

#define kTitleFontSize 15
#define kTitleTopBottomPadding 15
#define kTitleWidth LYScreenWidth() - kTitleTopBottomPadding * 2

#define kSEGDoorHeight 44
#define kSEGDoorWidth 167.5 * kScaleWidth
@implementation SEGDoorSettingNameView{
    
}
- (instancetype)initWithFrame:(CGRect)frame WithName:(NSString*)name WithType:(SEGEnumDoorSettingType)type{
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.height = kSEGDoorHeight;
        frame.size.width = kSEGDoorWidth;
    }
    self = [super initWithFrame:frame];
    self.exclusiveTouch = YES;
    self.layer.cornerRadius = 2.5;
    self.layer.masksToBounds = YES;
    self.backgroundColor = kColor_bg;
    
    [self addSubview:self.backImageView];
    [self addSubview:self.nameLabel];
    [self addSubview:self.editImageView];
    
    self.backImageView.hidden = YES;
    NSString *str = @"icon_set_select";
    switch (type) {
        case SEGEnumDoorSettingType_NO:
            str = @"icon_set_select";
            break;
        case SEGEnumDoorSettingType_Del:
            str = @"icon_set_del";
            break;
        case SEGEnumDoorSettingType_Add:
            str = @"icon_set_add";
            break;
        case SEGEnumDoorSettingType_Virtual:
            self.backImageView.hidden = NO;
            self.backgroundColor = kColor_W;
            self.backImageView.frame = self.frame;
            return self;
            break;
        default:
            break;
    }
    
    self.nameLabel.hidden = NO;
    self.editImageView.hidden = NO;
    
    [self.editImageView setImage:[UIImage imageWithContentsOfFile:SEGPicName(str)]];
    self.editImageView.frame = CGRectMake(self.width - 10 - 13, (self.height - 13) *0.5, 13, 13);
    self.editImageView.layer.cornerRadius = self.editImageView.width * 0.5;
    self.editImageView.layer.masksToBounds = YES;
    
    self.nameLabel.frame = CGRectMake(10, 0, self.width - 10 - 13 - 10 -10, self.height);
    self.nameLabel.text = name;
    self.backImageView.frame = self.frame;
    return self;
}

-(UIImageView *)editImageView
{
    if (!_editImageView) {
        _editImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"icon_set_select")]];
        _editImageView.hidden = YES;
    }
    return _editImageView;
}

-(UIImageView *)backImageView
{
    if (!_backImageView) {
        _backImageView = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:SEGPicName(@"bg_commondoor")]];
        _backImageView.hidden = YES;
    }
    return _backImageView;
}

-(UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [UILabel new];
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        _nameLabel.font = [UIFont systemFontOfSize:13];
        _nameLabel.textColor = kColor_1;
        _nameLabel.numberOfLines = 2;
        _nameLabel.hidden = YES;
    }
    return _nameLabel;
}
@end


@implementation SEGDoorSettingLayout
- (instancetype)initWithData:(NSArray<SEGNewMyDoorListModel*>*)data WithTitle:(NSString *)title WithDesc:(NSString *)desc{
    if (self == [super init]){
        _data = data;
        _title = title;
        _desc = desc;
        [self layout];
    }
    
    return self;
}
-(void)layout{
    _titleTextLayout = nil;
    _titleHeight = 0;
    
    [self layoutTitle];
}

-(void)layoutTitle{
    NSMutableAttributedString *text = [SEGKitHelper seg_TextWithString:_title fontSize:kTitleFontSize textColor:kColor_1];
    
    
    if (_desc.length > 0) {
        NSMutableAttributedString *one = [SEGKitHelper seg_TextWithString:_desc fontSize:kTitleFontSize textColor:kColor_3];
        [text appendAttributedString:one];
    }
    
    WBTextLinePositionModifier *modifier = [WBTextLinePositionModifier new];
    modifier.font = [UIFont fontWithName:@"Heiti SC" size:kTitleFontSize];
    modifier.paddingTop = kTitleTopBottomPadding;
    modifier.paddingBottom = kTitleTopBottomPadding;
    
    SEGTextContainer *container = [SEGTextContainer new];
    container.size = CGSizeMake(kTitleWidth, HUGE);
    container.linePositionModifier = modifier;
    
    _titleTextLayout = [SEGTextLayout layoutWithContainer:container text:text];
    _titleHeight = [modifier heightForLineCount:_titleTextLayout.rowCount];
}
@end

@implementation SEGSettingDoorView{
    SEGLabel *titleLabel;
    UIScrollView *scrollView;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (frame.size.width == 0 && frame.size.height == 0) {
        frame.size.width = LYScreenWidth();
        frame.size.height = 10;
    }
    self = [super initWithFrame:frame];
    self.backgroundColor = kColor_W;
    self.exclusiveTouch = YES;
    
    titleLabel = [SEGLabel new];
    titleLabel.userInteractionEnabled = NO;
    titleLabel.textVerticalAlignment = SEGTextVerticalAlignmentTop;
    titleLabel.displaysAsynchronously = YES;
    titleLabel.ignoreCommonProperties = YES;
    titleLabel.fadeOnAsynchronouslyDisplay = NO;
    titleLabel.fadeOnHighlight = NO;
    [self addSubview:titleLabel];
    
    scrollView = [[UIScrollView alloc]initWithFrame:CGRectZero];
    [self addSubview:scrollView];
    return self;
}

-(void)setLayout:(SEGDoorSettingLayout*)layout canScroll:(BOOL)canScroll{
    titleLabel.textLayout = layout.titleTextLayout;
    CGFloat _height = 0;
    
    if (layout.titleHeight > 0) {
        titleLabel.frame = CGRectMake(kTitleTopBottomPadding, 0, kTitleWidth, layout.titleHeight);
        _height += layout.titleHeight;
    }
    
    [scrollView removeAllSubviews];
    __block CGFloat contentHeight = 0;
    [layout.data enumerateObjectsUsingBlock:^(SEGNewMyDoorListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        SEGDoorSettingNameView *button = [[SEGDoorSettingNameView alloc]initWithFrame:CGRectZero WithName:obj.name WithType:obj.type];
        button.tag = idx;
        [scrollView addSubview:button];
        __weak typeof(self) _self = self;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(SEGDoorSettingNameView*  _Nonnull sender) {
            if ([_self.delegate respondsToSelector:@selector(seg_clickDoor:AtIndex:)]) {
                [_self.delegate seg_clickDoor:obj AtIndex:idx];
            }
        }];
        [button addGestureRecognizer:tap];
        
        NSInteger X = (idx%2);
        NSInteger Y = (idx/2);
        [button setFrame:CGRectMake(X* kSEGDoorWidth + X * (10 * kScaleWidth),
                                    (Y * kSEGDoorHeight) + Y * 10,
                                    kSEGDoorWidth,
                                    kSEGDoorHeight)];
        
        contentHeight +=  (X == 0 ? kSEGDoorHeight + (idx == layout.data.count - 2 ? 15 : 10 ) : 0);
    }];
    
//    scrollView.backgroundColor = kSegRandomColor;
    if (!canScroll) {
        scrollView.frame = CGRectMake(kTitleTopBottomPadding, _height, self.width - kTitleTopBottomPadding * 2, contentHeight);
        _height += contentHeight;
        self.height = _height;
    }else{
        scrollView.frame = CGRectMake(kTitleTopBottomPadding, _height, self.width - kTitleTopBottomPadding * 2, self.height - _height);
        scrollView.contentSize = CGSizeMake(scrollView.width, contentHeight);
    }
}

@end

